type User = {
  displayName: string;
  isAdmin?: boolean;
};

const user: User = {
  displayName: "Prof. Jayce Kunze",
};

type Credentials = {
  email: string;
  password: string;
};

const credentials: Credentials = {
  email: "Toy.Buster@yahoo.com",
  password: "cappie-aver-ophis-cotuit",
};

// 函数返回类型必须是User和Credentials
function getUserInfo(): User & Credentials {
  return {
    // or ...user
    displayName: "Ms. Juana Schuppe MD",
    // ...credentials,
    // 有时候需要对属性进行额外的操作可以用这种方法，对象合并，这样子email就是一个对象了
    email: Object.assign("Toy.Buster@yahoo.com", {
      primary: "hello",
    }),
    password: "",
  };
}

const result3 = getUserInfo();
/**
 * (property) email: "Toy.Buster@yahoo.com" & {
    primary: string;
  }
 */
console.log(result13.email.primary);

console.log(getUserInfo());

//交集操作挑战
const obj1 = { one: 1, two: 2 };
const obj2 = { three: 3, four: 4 };

// T extends object 约定你T必须也要包含object属性，也就是说 你T必须要是一个对象
function mergeObjects<T extends object, U extends object>(
  obj1: T,
  obj2: U
): T & U {
  return {
    ...obj1,
    ...obj2,
  };
}

const mergedObj = mergeObjects(obj1, obj2);
console.log(mergedObj);

export {};
